<template>
  <!-- 会议总览 -->
  <el-dialog
    width="30%"
    :modal="false"
    custom-class="meetingDialogBackGround"
    :visible.sync="groupCallDialog"
    :close-on-click-modal="false"
    :before-close="handleCancel"
  >
    <div slot="title" style="color:white;text-align:center">
      <span>会议总览</span>
    </div>
    <div class="meetingDialog">
      <el-scrollbar style="height:100%;">
        <div class="meetingList">
          <div
            v-for="item of meet_list"
            :key="item.meetID"
            @click="handleSelectMeet(item.meetID)"
            :class="
            selectMeetID == item.meetID ? 'meetList selectMeet' : 'meetList'
          "
          >
            <img class="meetImg" src="../../assets/images/allowspeak.png" />
            <div class="meetTxt">{{ item.meetName }}</div>
          </div>
        </div>
      </el-scrollbar>
    </div>
    <div slot="footer" class="dialog-footer">
      <el-button
        @click="
            createMeetFlag = true;
            selectMeetID = '';
            createMeetForm.meetName = '';
          "
        style="float:left"
      >新建</el-button>
      <el-button @click="handleMakeGroup">确定</el-button>
    </div>
    <!-- 新建会议 -->
    <el-dialog
      :modal="false"
      custom-class="newMeetingDialogBackGround"
      width="30%"
      :visible.sync="createMeetFlag"
      :close-on-click-modal="false"
    >
      <div slot="title" style="color:white">
        <span>新建会议</span>
      </div>
      <div class="newMeetingDialog">
        <el-form :model="createMeetForm" label-width="80px" style>
          <el-form-item label="会议名称" class="fromItem">
            <el-input v-model="createMeetForm.meetName"></el-input>
          </el-form-item>
          <el-form-item label="默认发言" class="fromItem">
            <el-switch v-model="createMeetForm.isAllowSpeak"></el-switch>
          </el-form-item>
        </el-form>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button style="float:left" @click="createMeetFlag = false">取消</el-button>
        <el-button @click="handleCreateMeet">确定</el-button>
      </div>
    </el-dialog>
  </el-dialog>
</template>

<script>
import { getMeetinfo, makeCall } from '@/api/data'
export default {
  props: {
    title: String,
    groupCallDialog: Boolean,
    groupOrSelect: String,
    callingDevice: String,
    selectGroupID: String,
    selectUsers: Array,
    thisSelectMember: Object,
  },
  data() {
    return {
      modeOptions: [
        { label: '视频会议', value: 'video' },
        { label: '语音会议', value: 'audio' },
      ],
      rules: {
        meetName: [
          { required: true, message: '会议名称不能为空', trigger: 'blur' },
        ],
        isAllowSpeak: [
          { required: true, message: '会议类型不能为空', trigger: 'blur' },
        ],
        meetMode: [
          { required: true, message: '默认发言不能为空', trigger: 'blur' },
        ],
      },
      loading: false,
      selectMeetID: '',
      meet_list: [],
      createMeetFlag: false,
      createMeetForm: {
        meetName: '',
        isAllowSpeak: true,
      },
    }
  },
  methods: {
    init() {
      let _this = this
      getMeetinfo().then((res) => {
        let meet_list = res.data.meet_list.filter(
          (e) => e.meetMode == 'audio' || e.meetMode == 'video'
        )
        meet_list.forEach((e) => {
          if (e.meetMode == 'video') {
            e.meetName = e.meetName + '(视频会议)'
          }
        })
        this.meet_list = meet_list
        this.selectMeetID = ''
        //默认选中第一个会议
        _this.handleSelectMeet(this.meet_list[0].meetID)
        //在只剩一个语音会场时发起选呼或者组呼，跳过会场选择
        if (meet_list.length == 1) {
          this.handleMakeGroup()
        }
      })
    },
    //选中会议事件
    handleSelectMeet(meetID) {
      this.selectMeetID = meetID
    },
    //选择会议进入组呼，选呼，群答
    handleMakeGroup() {
      let _this = this
      if (this.selectMeetID == '') {
        this.$message.warning('请选择会议')
        return
      }
      //视频会商页面联动参数--开始
      let meetArr = this.meet_list.filter((e) => {
        return e.meetID == _this.selectMeetID
      })
      if (meetArr.length == 0) {
        this.$message.warning('选中会议异常，请重试')
        return
      }
      sessionStorage.setItem('currentMeeting', JSON.stringify(meetArr[0]))
      //联动结束
      this.$emit('update:groupCallDialog', false)
      if (this.groupOrSelect == 'group') {
        //组呼
        makeCall({
          request: {
            '@id': '100000',
            '@type': 'GroupCall',
            callingDevice: this.callingDevice,
            groupID: this.selectGroupID,
            meetID: this.selectMeetID,
            callMode: 'parallel',
          },
        })
        this.handleCallBack('groupCall')
      } else if (this.groupOrSelect == 'select') {
        //选呼
        const called = this.selectUsers.map((e) => ({
          calledDevice: e.calledDevice,
          userID: null,
        }))
        makeCall({
          request: {
            '@id': '100000',
            '@type': 'SelectCall',
            callingDevice: this.callingDevice,
            meetID: this.selectMeetID,
            callMode: 'parallel',
            called,
          },
        })
        this.handleCallBack('selectCall')
      } else if (this.groupOrSelect == 'GroupAnswerCall') {
        //群答
        makeCall({
          request: {
            '@id': '100000',
            '@type': 'GroupAnswerCall',
            callingDevice: this.callingDevice,
            meetID: this.selectMeetID,
            isSpeak: '1',
          },
        })
        this.handleCallBack('GroupAnswerCall')
      } else {
        //点击头像加入会议 选呼
        if (
          this.thisSelectMember.defaultNo == '' ||
          this.thisSelectMember.defaultNo == undefined ||
          this.thisSelectMember.defaultNo == null
        ) {
          this.$message.warning('选择成员为空')
          return
        }
        let called = {
          calledDevice: this.thisSelectMember.defaultNo,
          userID: null,
        }
        makeCall({
          request: {
            '@id': '100000',
            '@type': 'SelectCall',
            callingDevice: this.callingDevice,
            meetID: this.selectMeetID,
            callMode: 'parallel',
            called,
          },
        })
        this.handleCallBack('')
      }
    },
    //创建会议
    handleCreateMeet() {
      if (this.createMeetForm.meetName == '') {
        this.$message.warning('请填写会议名称')
        return
      }
      makeCall({
        request: {
          '@id': '100000',
          '@type': 'CreateMeet',
          meetName: this.createMeetForm.meetName,
          meetNum: this.createMeetForm.meetName,
          isAllowSpeak: this.createMeetForm.isAllowSpeak ? 'yes' : 'no',
          meetMode: 'audio',
        },
      })
      this.createMeetFlag = false
      setTimeout(() => {
        this.init()
      }, 1500)
    },
    // 取消按钮
    handleCancel() {
      this.$emit('update:groupCallDialog', false)
    },
    //回调
    handleCallBack(type) {
      this.$emit('handleGroupCallBack', type)
    },
  },
  mounted() {
    this.init()
  },
}
</script>

<style lang="scss" scoped>
/deep/.meetingDialogBackGround {
  background-color: #1a4969 !important;

  .el-dialog__body {
    padding: 8px 10px;
    color: #606266;
    font-size: 14px;
    word-break: break-all;
  }

  .el-dialog__footer {
    padding: 0px 17px 13px;
    text-align: right;
    -webkit-box-sizing: border-box;
    box-sizing: border-box;
  }

  .el-button {
    border: 1px solid white;
    background-color: #1a4969 !important;
    color: #eef0f3;
  }
  .meetingDialog {
    height: 500px;
    .meetingList {
      border: 1px solid #ffffff;
      height: 500px;
    }
  }
  @media screen and (max-width: 1024px) {
    .meetingDialog {
      height: 350px;
      .meetingList {
        height: 350px;
      }
    }
  }
  .selectMeet {
    background-color: #3399ff;
    color: #fff;
  }
  .meetList {
    height: 10%;
    border: 1px solid #ccc;
    margin-bottom: 10px;
    display: flex;
    color: white;
    margin-top: 3px;
    margin-left: 3px;
    margin-right: 3px;
    border-radius: 10px;
  }
  .meetImg {
    width: 9%;
    margin-right: 10px;
    margin-left: 10px;
  }

  .meetTxt {
    line-height: 30px;
    font-size: 15px;
    text-align: center;
  }
}
/deep/.newMeetingDialogBackGround {
  background-color: #1a4969 !important;
  .el-button {
    border: 1px solid white;
    background-color: #1a4969 !important;
    color: #eef0f3;
  }
  .el-dialog__body {
    padding: 8px 10px;
    color: #606266;
    font-size: 14px;
    word-break: break-all;
  }
  .el-dialog__footer {
    padding: 0px 17px 13px;
    text-align: right;
    -webkit-box-sizing: border-box;
    box-sizing: border-box;
  }
  .newMeetingDialog {
    height: 500px;
  }
  .fromItem {
    color: white;
  }
  .fromItem .el-form-item__label {
    color: white;
  }
  @media screen and (max-width: 1024px) {
    .newMeetingDialog {
      height: 350px;
    }
  }
}
</style>

